# 会员信息管理
import os
import time
from datetime import datetime

from django.http import HttpResponse
from django.shortcuts import render

from myadmin.models import Member
from myadmin.utils import params_status, get_page


def index(request, pIndex=1):
    mlists, mywhere, kw = params_status(Member, request)

    mlists = mlists.order_by("id")
    list2, plist, pIndex, maxpages = get_page(mlists, pIndex, 5)
    context = {"memberlist": list2, 'plist': plist, 'pIndex': pIndex, 'maxpages': maxpages, 'mywhere': mywhere}
    return render(request, "myadmin/member/index.html", context)


def add(request):
    return render(request, "myadmin/member/add.html")

def insert(request):
    try:
        myfile = request.FILES.get("avatar", None)
        if not myfile:
            return HttpResponse("没有上传头像")
        avatar = str(time.time()) + "." + myfile.name.split('.').pop()
        destination = open("./static/uploads/member/" + avatar, "wb+")
        for chunk in myfile.chunks():
            destination.write(chunk)
        destination.close()

        ob = Member()
        ob.nickname = request.POST['nick']
        ob.mobile = request.POST['mobile']
        ob.avatar = avatar
        ob.status = 1
        ob.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': "添加成功！"}
    except Exception:
        context = {'info': "添加失败！"}
    return render(request, "myadmin/info.html", context)


def delete(request, uid=0):
    try:
        ob = Member.objects.get(id=uid)
        ob.status = 9
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': "删除成功！"}
    except Exception:
        context = {'info': "删除失败！"}
    return render(request, "myadmin/info.html", context)


def edit(request, uid=0):
    try:
        ob = Member.objects.get(id=uid)
        context = {'member': ob}
        return render(request, "myadmin/member/edit.html", context)
    except Exception:
        context = {'info': "没有找到要修改的信息！"}
        return render(request, "myadmin/info.html", context)


def update(request, uid):
    try:
        ob = Member.objects.get(id=uid)
        myfile = request.FILES.get("avatar", None)
        if myfile:
            avatar = str(time.time()) + "." + myfile.name.split('.').pop()
            destination = open("./static/uploads/member/" + avatar, "wb+")
            for chunk in myfile.chunks():
                destination.write(chunk)
            destination.close()
            os.remove("./static/uploads/member/" + ob.avatar)
            ob.avatar = avatar

        ob.nickname = request.POST['nickname']
        ob.mobile = request.POST['mobile']
        ob.status = request.POST['status']
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': "修改成功！"}
    except Exception:
        context = {'info': "修改失败！"}
    return render(request, "myadmin/info.html", context)
